var g_placeProxy = null;
var PlaceProxy = cc.Node.extend({
    _placeTowerOk : null,
    _placeTowerNo : null,
    //////////////////////////////
    _enableTowerMenu : null,
    //////////////////////////////
    _isShow  : false,
    ctor : function(){
        this._super();
        g_placeProxy=this;
        g_placeProxy.retain();
        this.initEnableTower();
    },
    onEnter:function () {
        this._super();
        this.addLocalFlag();
    },
    onExit : function(){
        this._super();
    },
    initEnableTower : function(){
        // getting tower config
        var towerObject = GetTowerObject();
        var towers = [];
        for( var i=0;i<towerObject.towers.length;i++){
            var data=towerObject.towers[i];
            var filename = MainResource(data.res[0]);
            var tower = new cc.MenuItemImage(filename,filename,this.onChooseTower,this);
            tower.setVisible( false );
            tower.setUserData( data );
            towers.push(tower);
            //////////////////////////////////////////
            this.addCostLabel(tower,cc.p(35,7),data.su[0]);
        }
        this._enableTowerMenu = cc.Menu.create( towers );
        this._enableTowerMenu.setPosition( cc.p(0,0) );
        this.addChild( this._enableTowerMenu );
    },
    addLocalFlag : function( ){
        var filename=MainResource("ok.png");
        this._placeTowerOk = new cc.Sprite( filename );
        this._placeTowerOk.setVisible(false);
        this.addChild(this._placeTowerOk);
        filename=MainResource("no.png");
        this._placeTowerNo = new cc.Sprite( filename );
        this._placeTowerNo.setOpacity(0);
        this.addChild(this._placeTowerNo);
    },
    addCostLabel : function(parent,pos,money){
        var filename = MainResource("place.png");
        var place = new cc.Sprite(filename);
        place.setPosition( pos );
        parent.addChild(place);
        filename = MainResource("num1.png");
        var label = cc.LabelAtlas.create(money, filename,10, 13, '0');
        label.setAnchorPoint(cc.p(0.5,0.5));
        label.setPosition( pos );
        parent.addChild(label);
    },
    /////////////////////////
    isShow : function(){
        return this._isShow;
    },
    ////////////////////////////////////////////
    showOkFlag : function(show,pos){
        if(show){
            this._placeTowerOk.setVisible( true );
            this._placeTowerOk.setPosition( cc.pAdd( pos,cc.p(35,35) )  );
            this._placeTowerOk.stopAllActions();
            this._placeTowerOk.runAction( new cc.RepeatForever(
                new cc.Sequence(
                    new cc.ScaleTo(0.3,1.15),
                    new cc.ScaleTo(0.2,1) )
            ) );
        }else{
            this._placeTowerOk.stopAllActions();
            this._placeTowerOk.setVisible( false );
        }
    },
    showNoFlag : function(show,pos){
        if(show){
            this._placeTowerNo.stopAllActions();
            this._placeTowerNo.setOpacity( 255 );
            this._placeTowerNo.setPosition( cc.pAdd( pos,cc.p(35,35) ) );
            this._placeTowerNo.runAction( new cc.FadeOut(1.2) );
        }else{
            cc.log("show can not place tower flag ...do nothing");
        }
    },
    // refresh enable towers
    refreshTower : function(gold){
        var children = this._enableTowerMenu.getChildren();
        if( children[0].isVisible() ){
            for ( var i=0;i<children.length;i++){
                var tower=children[i];
                if( tower ){
                    var data = tower.getUserData();
                    var cost=data.su[0];
                    if ( gold < cost){
                        this.setTowerEnable(tower,false);
                    }else{
                        this.setTowerEnable(tower,true);
                    }
                }
            }
        }
    },
    layoutTower:function(length,children,pixel){
        for ( var i=0;i<length;i++){
            var tower= children[i],line=140,tow=length>3?1:0;
            if( i<3 ){
                var buffer=length<3?length:3,tPos = cc.p(pixel.x-(buffer-2)*0.5*70+i*70,pixel.y+100);
                if( tPos.y>cc.winSize.height-line )tPos=cc.pAdd( tPos,cc.p(0,-135-tow*70) );
                tower.setPosition( tPos );
                tower.setVisible( true );
            }else{
                var index=i- 3,buffer=length- 3,tPos = cc.p(pixel.x-(buffer-2)*0.5*70+index*70,pixel.y-35);
                if( tPos.y<line )tPos=cc.pAdd( tPos,cc.p(0,205) );
                tower.setPosition( tPos );
                tower.setVisible( true );
            }
        }
    },
    // hide towers
    showPanel : function(show,pos){
        var children = this._enableTowerMenu.getChildren(),length=children.length;
        if( show ) {
            ////////////////////////////////////////
            var jsonObj = GetMapObject();
            var point = PixelToPoint(jsonObj,pos);
            var pixel = pointToPixel(jsonObj,point);
            if( MapEmptyPoint( point ) ){
                this.showOkFlag(true,pixel);
                ////////////////layout tower///////////////////////
                this.layoutTower(length,children,pixel);
                this._isShow = true;

                //////////////////////////////
                var iu = getUILayerInstance();
                this.refreshTower( iu.getMoney() );
            }else{
                cc.log("this point is not empty");
                this.showNoFlag(true,pixel);
            }
        }else {
            this.showOkFlag(false);
            this.showNoFlag(false);
            /////////////////////////////////////////
            for ( var i=0;i<length;i++){
                var tower= children[i];
                tower.setVisible( false );
            }
            this._isShow = false;
        }
    },
    /////////////////////////////////////////////////////////////////////////////////////
    // setting tower enable or not
    setTowerEnable : function(tower,enable){
        if(enable){
            tower.setColor(cc.color.WHITE);
        }else{
            tower.setColor(cc.color(135,123,103));
        }
    },
    // choose tower to locating
    onChooseTower : function(sender){
        cc.log("on tower choose");
        var iu = getUILayerInstance();
        var data = sender.getUserData();
        var cost = data.su[0];
        if ( iu.getMoney()>=cost){
            iu.addMoney( -cost );
            var pos = this._placeTowerOk.getPosition();
            // add tower
            AddTowerIntoTowerVessel(data,pos);
            // invisible tower menu
            this.showPanel(false);
        }else{
            cc.log("no money");
        }
    }
});

////////////////////////////////////////////////////
RefreshTower=function(gold){
    g_placeProxy.refreshTower(gold);
    cc.log("refresh choose tower");
}
ShowPlacePanel=function(pos){
    var show = g_placeProxy.isShow();
    cc.log("show place panel: "+!show);
    g_placeProxy.showPanel(!show,pos);

    //////////////////////////////////////////
    g_updateProxy.showPanel(false);
}